//
//  Optional+Extensions.swift
//  Signup
//
//  Created by heze on 2018/10/9.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation

enum DifferentiatorError : Error {
    case unwrappingOptional
    case preconditionFailed(message: String)
}

func precondition(_ condition: Bool, _ message: @autoclosure() -> String) throws -> () {
    if condition {
        return
    }
    debugFatalError("Precondition failed")
    
    throw DifferentiatorError.preconditionFailed(message: message())
}

func debugFatalError(_ error: Error) {
    debugFatalError("\(error)")
}

func debugFatalError(_ message: String) {
    #if DEBUG
    fatalError(message)
    #else
    print(message)
    #endif
}


extension Optional {
    
    func unwrap() throws -> Wrapped {
        
        if let unwrapped = self {
            return unwrapped
        }
        else {
            debugFatalError("Error during unwrapping optional")
            throw DifferentiatorError.unwrappingOptional
        }
    }
}
